package com.itouch.bracelet.adapter;

import java.util.ArrayList;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.itouch.bracelet.R;
import com.itouch.bracelet.bean.Contact;
import com.itouch.widget.PinnedSectionListView.PinnedSectionListAdapter;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.assist.ImageScaleType;
import com.nostra13.universalimageloader.core.display.RoundedBitmapDisplayer;

/**
 * cocoaSJ
 */
public class ContactAdapter extends BaseAdapter implements
		PinnedSectionListAdapter {
	private ArrayList<Contact> list;
	private Context context;
	private int itemColor;
	private int headColor;
	private int textColor;
	private LayoutInflater inflater;
	private ImageLoader imgImageLoader = ImageLoader.getInstance();
	private DisplayImageOptions options;

	public ContactAdapter(Context context, ArrayList<Contact> list) {
		this.list = list;
		this.context = context;
		itemColor = Color.parseColor("#FFFFFF");
		headColor = Color.parseColor("#fee9e6");
		textColor = Color.parseColor("#e8615e");
		inflater = LayoutInflater.from(context);
		options = new DisplayImageOptions.Builder()
				.imageScaleType(ImageScaleType.EXACTLY_STRETCHED)
				.showImageOnLoading(R.drawable.default_face)
				.showImageForEmptyUri(R.drawable.default_face)

				.displayer(new RoundedBitmapDisplayer(5))
				.showImageOnFail(R.drawable.default_face).cacheInMemory(true)
				.cacheOnDisc(true).considerExifParams(true)
				.bitmapConfig(Bitmap.Config.RGB_565).build();
	}

	@Override
	public int getCount() {
		return list.size();
	}

	@Override
	public Contact getItem(int position) {
		return list.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(int position, View converView, ViewGroup viewGrop) {
		// TODO Auto-generated method stub
		ViewHolder vh = null;
		if (converView == null) {
			vh = new ViewHolder();
			converView = inflater.inflate(R.layout.adapter_item_contact,
					viewGrop, false);
			vh.text = (TextView) converView.findViewById(R.id.contact_text);
			vh.image = (ImageView) converView.findViewById(R.id.contact_img);
			converView.setTag(vh);
		} else {
			vh = (ViewHolder) converView.getTag();
		}
		Contact bean = getItem(position);
		vh.text.setText(bean.getReceiverName());
		vh.text.setTextColor(textColor);
		if (bean.getId() == null && bean.getId() == null) {
			vh.text.setBackgroundColor(headColor);
			vh.text.setTextSize(20);
			vh.text.setPadding(8, 5, 5, 5);
			vh.image.setVisibility(View.GONE);

		} else {
			vh.text.setBackgroundResource(R.drawable.comm_bg_press);
			vh.text.setTextSize(16);
			vh.text.setPadding(10, 5, 5, 5);

			vh.image.setVisibility(View.VISIBLE);
			imgImageLoader.displayImage(bean.getHeadPortraitUrl(), vh.image,
					options);

		}
		return converView;
	}

	@Override
	public boolean isItemViewTypePinned(int viewType) {
		return viewType == 0;// 0是标题，1是内容
	}

	@Override
	public int getViewTypeCount() {
		return 2;// 2种view的类型 baseAdapter中得方法
	}

	@Override
	public int getItemViewType(int position) {
		Contact bean = getItem(position);
		// 0是标题，1是内容
		if (bean.getId() == null && bean.getReceiverName() == null) {
			return 0;
		}
		return 1;
	}

	public void refresh(ArrayList<Contact> arr) {
		list.clear();
		list.addAll(arr);
		notifyDataSetChanged();
	}

	class ViewHolder {
		public TextView text;
		public ImageView image;
	}

}
